using System;
using System.Collections.Generic;

namespace Beetle;

public class ChannelDisposedQueue : Task
{
	private new Queue<ChannelDisposedEventArgs> m_a = new Queue<ChannelDisposedEventArgs>(1024);

	private static ChannelDisposedQueue m_b = new ChannelDisposedQueue();

	public static ChannelDisposedQueue DisposedQueue => ChannelDisposedQueue.m_b;

	public void Add(ChannelDisposedEventArgs channel)
	{
		lock (this.m_a)
		{
			this.m_a.Enqueue(channel);
		}
	}

	private void b()
	{
		lock (this.m_a)
		{
			if (this.m_a.Count > 0)
			{
				this.m_a.Dequeue();
			}
		}
	}

	private new ChannelDisposedEventArgs a()
	{
		lock (this.m_a)
		{
			if (this.m_a.Count > 0)
			{
				return this.m_a.Peek();
			}
			return null;
		}
	}

	protected override void Execute()
	{
		ChannelDisposedEventArgs channelDisposedEventArgs = a();
		while (channelDisposedEventArgs != null && Environment.TickCount - channelDisposedEventArgs.a > channelDisposedEventArgs.Delay)
		{
			((aq)channelDisposedEventArgs.Channel).c();
			channelDisposedEventArgs.Channel = null;
			b();
			channelDisposedEventArgs = a();
		}
	}
}
